<?php
error_reporting(0);
$isadmin=1;
include_once '../comm/config.php';
include '../comm/msgset.php';
include '../comm/checkpostandget.php';
include 'check.php';
define('BIAOTOU',$BIAOTOU);
function select($table, $sel_field, $where, $alert = 0) {
	if (strstr($table, ',') != '') {
		$table = str_replace(',', ',' . BIAOTOU, $table);
	}
	$sql = "select $sel_field from " . BIAOTOU . $table . " where $where limit 1";
	if ($alert == 1) {
		echo $sql;
	}
	$query = mysql_query($sql);
	if ($query != '') {
		while ($row = mysql_fetch_array($query)) {
			if (strpos($sel_field, ",") !== false or strpos($sel_field, "*") !== false) {
				$arr = $row;
			} else {
				if (strpos($sel_field, ".") !== false) {
					$sel_field = preg_replace('/^(.*)\./', '', $sel_field);
				}
				$arr = $row[$sel_field];
			}
		}
	}
	return $arr;
}

$id=(int)$_POST['id'];
$trade=select('tradelist','id,outer_code,checked,fxje,trade_id,commission,outer_code,checked','Id="'.$id.'" and checked<>-1');
$trade_id=$trade['trade_id'];
$fxje=$trade['fxje'];
$jifen=$fxje*JIFENBL;
$tgyj=$trade['commission']*TGBL;
$sj=date('Y-m-d H:i:s');

if($trade['id']>0){
    $user=select('user','ddusername,tjr,txstate,money,dhstate,email','id="'.$trade['outer_code'].'"');
	$ddusername=$user['ddusername'];
	$why='您的淘宝订单'.$trade_id.'发生退款，所以取消本次提现';
	if($ddusername!='' && $trade['checked']==2){ //会员得到返利，剪掉金额，积分，等级
	    $sql="update ".BIAOTOU."user set money=money-$fxje,jifen=jifen-$jifen,dengji=dengji-1 where Id='".$trade['outer_code']."' limit 1"; //减去金额积分等级
		mysql_query($sql);
		
		$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('退款订单','您的淘宝订单".$trade_id."已退款','$sj',0,'".$ddusername."','网站客服');";//用户消息
		mysql_query($sql);
		
		$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,jifen,addtime,memo) values ('".$ddusername."','退款订单',".-$fxje.",".-$jifen.",'$sj','交易号".$trade_id."')";//用户明细
		mysql_query($sql);
		
		if($user['txstate']==1 && $user['money']-$fxje<0){ //如果会员处在提现中并且现有金额小于退款金额，取消提现状态
		    $tixian=select('tixian','Id,txje','ddusername="'.$ddusername.'" order by id desc');
			$txje=$tixian['txje'];
			$sql="update ".BIAOTOU."user set money=money+$txje,txstate=0 where Id='".$trade['outer_code']."' limit 1"; //退回提现
			mysql_query($sql);
			$msg_tixianfail=str_replace("[why]",$why,$msg_tixianfail);
		    $insert = "INSERT into ".$BIAOTOU."msg(ddusername,title,content,addtime,senduser) values ('$ddusername','提现失败','".$msg_tixianfail."','$sj','网站客服')";
		    mysql_query($insert);
			
			$sql = "update ".BIAOTOU."tixian set txstate=2 where Id=".$tixian['Id'];
		    mysql_query($sql);
		}
		
		if($tgyj>0 && $user['tjr']>0){
			$tjrname=select('user','ddusername','Id="'.$user['tjr'].'"');
		    $sql="update ".BIAOTOU."user set money=money-$tgyj where Id='".$user['tjr']."' limit 1"; //减去推广佣金
			mysql_query($sql);
			$sql="insert into ".BIAOTOU."mingxi (ddusername,shijian,je,jifen,addtime,memo) values ('".$tjrname."','退款订单',".-$tgyj.",0,'$sj','交易人".$ddusername."')";//用户明细
		    mysql_query($sql);
		}
	}
	$sql="update ".BIAOTOU."tradelist set checked=-1 where id='".$id."'";
	mysql_query($sql);
}
admin_log('退款订单');
header('Location: tradelist.php?qr=-1'); 

